/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant;


import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * <p>
 * This class is the workflow that is used when attemping to
 * fetch artifacts.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class Workflow {

    private List processes = new ArrayList();


    /**
     * Constructs a new <code>WorkflowType</code>.
     */
    public Workflow() {
    }


    /**
     * Add a new process to the list of processes that compose this workflow.
     *
     * @param   process The new process
     */
    public void addProcess(Process process) {
        processes.add(process);
    }

    /**
     * Attempts to resolve the artifact dependencies for the given artifact by
     * first checking the local cache and then using the process's in order until
     * it is found.
     */
    public void resolveArtifactDependencies(Artifact artifact,
            LocalCacheStore localCache)
    throws SavantException {
        boolean done = localCache.resolveArtifactDependencies(artifact);
        if (!done) {
            Iterator iter = processes.iterator();
            Process process;
            while (iter.hasNext()) {
                process = (Process) iter.next();
                done = process.resolveArtifactDependencies(artifact, localCache);
                if (done) {
                    break;
                }
            }
        }
    }

    /**
     * This first checks the local cache for the artifact. If it doesn't find it
     * there, it loops over all the processes until the artifact is found or not.
     * It is the responsibility of the {@link Process} object that finds the
     * artifact to copy it to the local cache.
     *
     * @param   artifact The artifact to locate
     * @param   localCache The local cache where the artifact is cached
     * @return  The artifact if found or null
     * @throws  SavantException If any process in the workflow fails
     */
    public File findArtifact(Artifact artifact, LocalCacheStore localCache)
    throws SavantException {
        File file = localCache.find(artifact);
        if (file != null) {
            return file;
        }

        Iterator iter = processes.iterator();
        Process process;
        while (iter.hasNext()) {
            process = (Process) iter.next();
            file = process.fetch(artifact, localCache);
            if (file != null) {
                break;
            }
        }

        return file;
    }

    /**
     * Validates the process objects.
     */
    public void validate() throws SavantException {
        Iterator iter = processes.iterator();
        Process process;
        while (iter.hasNext()) {
            process = (Process) iter.next();
            process.validate();
        }
    }
}